//
//  MQLogonVCOfMinDy.m
//  MinDyNews
//
//  Created by xinfuyidaodai on 16/7/16.
//  Copyright © 2016年 MQ. All rights reserved.
//

#import "MQLogonVCOfMinDy.h"
#import "RegisterVCOfFVQM.h"
#import "MyViewController.h"
#import "AddressViewController.h"
#import "AppDelegate.h"



@interface MQLogonVCOfMinDy ()<UIApplicationDelegate>

@property (nonatomic, retain)UILabel *labelOfTitle;
@property (nonatomic, retain)UILabel *labelOfDL;
@property (nonatomic, retain)UILabel *labelZC;

@property (nonatomic, retain)UITextField *textFieldOfZH;

@property (nonatomic, retain)UITextField *textFieldOfMM;
@property (nonatomic, retain)UIView *viewOfZH;
@property (nonatomic, retain)UIView *viewOfMM;
@property (nonatomic, retain)UIButton *buttonOfLogin;
@property (nonatomic, retain) UIButton *buttonOfZC;

@property (nonatomic, retain)UILabel *labelOfSanFang;
@property (nonatomic, retain)UIButton *buttonOfWB;
@property (nonatomic, retain)UIButton *buttonOfWX;
@property (nonatomic, retain)UIButton *buttonOfQQ;

@property (nonatomic, retain) UIImageView *imageViewOfBackground;

@property (nonatomic, retain) UIView *viewOfShadow;//加暗度，让标题显现，高亮


@end

@implementation MQLogonVCOfMinDy




- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self CreateDL];
    [self ThridConfig];
    
    
    self.navigationController.navigationBarHidden = YES;
  
}
#pragma mark - 顶部配置

-(void)config{
    
    
    self.imageViewOfBackground = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_imageViewOfBackground];
    
    self.imageViewOfBackground.image = [UIImage imageNamed:@"beijin"];
    
    
    self.viewOfShadow = [[UIView alloc]init];
    [self.imageViewOfBackground addSubview:_viewOfShadow];
    self.viewOfShadow.backgroundColor = [UIColor colorWithRed:38 / 255.0 green:38 / 255.0 blue:38 / 255.0 alpha:0.2];
    [self.viewOfShadow mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.equalTo(self.imageViewOfBackground);
        
    }];

    
    
    self.labelOfDL = [[UILabel alloc]init];
    [self.view addSubview:_labelOfDL];
    self.labelOfDL.text = @"登录";
    self.labelOfDL.font = [UIFont systemFontOfSize:15];
    self.labelOfDL.textColor = [UIColor whiteColor];
    
    [self.labelOfDL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).with.offset(30);
        make.left.equalTo(self.view).with.offset(30);
        make.width.offset(30);
        make.height.offset(30);
        
    }];

    
    
    self.labelOfTitle = [[UILabel alloc]init];
    [self.view addSubview:_labelOfTitle];

    self.labelOfTitle.text = @"快信欢迎你";
    self.labelOfTitle.font = [UIFont systemFontOfSize:15];
    self.labelOfTitle.textColor = [UIColor whiteColor];
    self.labelOfTitle.textAlignment = NSTextAlignmentCenter;
    
    [self.labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).with.offset(30);
        make.centerX.equalTo(self.view);
        make.width.offset(90);
        make.height.offset(30);
        
    }];
        
    self.labelZC = [[UILabel alloc]init];
    [self.view addSubview:_labelZC];
    self.labelZC.text = @"注册";
    self.labelZC.font = [UIFont systemFontOfSize:15];
    self.labelZC.textColor = [UIColor whiteColor];
    
    
    
    [self.labelZC mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).with.offset(30);
        make.right.equalTo(self.view).with.offset(-30);
        make.width.offset(30);
        make.height.offset(30);
        
    }];

    
}

#pragma mark - 中间登录配置


-(void)CreateDL{
    
    /**账号*/
    self.textFieldOfZH = [[UITextField alloc]init];
    [self.view addSubview:_textFieldOfZH];
    self.textFieldOfZH.placeholder = @"输入快信邮箱/用户名";
    self.textFieldOfZH.font = [UIFont systemFontOfSize:12];
     [self.textFieldOfZH setValue:[UIColor colorWithRed:200 / 256.0  green:200/ 256.0  blue: 200 / 256.0  alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
    
    self.textFieldOfZH.textColor = [UIColor whiteColor];
    
    [self.textFieldOfZH mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).with.offset(150);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(@30);
        
    }];
    
    
    self.viewOfZH = [[UIView alloc]init];
    [self.view addSubview:_viewOfZH];
    self.viewOfZH.backgroundColor = [UIColor whiteColor];
    [self.viewOfZH mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.textFieldOfZH.mas_bottom).with.offset(1);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(@2);
        
        
    }];
    
    
    
    /**密码*/
    self.textFieldOfMM = [[UITextField alloc]init];
    [self.view addSubview:_textFieldOfMM];
    self.textFieldOfMM.placeholder = @"输入快信密码";
    self.textFieldOfMM.font = [UIFont systemFontOfSize:12];
    [self.textFieldOfMM setValue:[UIColor colorWithRed:200 / 256.0  green:200 / 256.0  blue: 200 / 256.0  alpha:1]forKeyPath:@"_placeholderLabel.textColor"];
    self.textFieldOfMM.textColor = [UIColor whiteColor];
    
    [self.textFieldOfMM mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewOfZH.mas_bottom).with.offset(20);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(@30);
        
    }];
    
    
    self.viewOfMM = [[UIView alloc]init];
    [self.view addSubview:_viewOfMM];
    self.viewOfMM.backgroundColor = [UIColor whiteColor];
    [self.viewOfMM mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.textFieldOfMM.mas_bottom).with.offset(1);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(@2);
        
    }];
    
    
    
    /**登录*/
    self.buttonOfLogin = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_buttonOfLogin];
    [self.buttonOfLogin setTitle:@"登录" forState: UIControlStateNormal];
    [self.buttonOfLogin addTarget:self action:@selector(ClickLogin) forControlEvents: UIControlEventTouchUpInside];
    self.buttonOfLogin.tintColor = [UIColor whiteColor];
    self.buttonOfLogin.backgroundColor = [UIColor whiteColor];
    
     self.buttonOfLogin.titleLabel.font = [UIFont systemFontOfSize:12];
    self.buttonOfLogin.tintColor  =[UIColor colorWithRed:81 / 256.0  green:154 / 256.0  blue: 163 / 256.0  alpha:1];
    self.buttonOfLogin.layer.cornerRadius = 10;
    [self.buttonOfLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewOfMM.mas_bottom).with.offset(80);
        make.centerX.equalTo(self.view);
        make.width.equalTo(@120);
        make.height.equalTo(@30);
        
    }];
    
    
    self.buttonOfZC = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_buttonOfZC];
    
    [self.buttonOfZC setTitle:@"注册" forState:UIControlStateNormal];
    self.buttonOfZC.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [self.buttonOfZC addTarget:self action:@selector(ClickZuCe:) forControlEvents: UIControlEventTouchUpInside];
    self.buttonOfZC.tintColor = [UIColor colorWithRed:81 / 256.0  green:154 / 256.0  blue: 163 / 256.0  alpha:1];
    self.buttonOfZC.backgroundColor = [UIColor whiteColor];
    self.buttonOfZC.layer.masksToBounds = YES;
    self.buttonOfZC.layer.cornerRadius = 15;
    [self.buttonOfZC mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewOfMM.mas_bottom).with.offset(80);
        make.left.equalTo(self.buttonOfLogin.mas_right).with.offset(10);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
    }];

    
    
}


//自动登录：登录成功之后，将用户名和密码存储在本地数据库中，下次打开程序的时候直接取出数据库中的账号和密码进行登录

//环信已经帮我们做好了，我们只需要设置一个属性就可以了，属性应该在登录成功之后设置
-(void)ClickLogin{
    
    
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:_textFieldOfZH.text password:_textFieldOfMM.text completion:^(NSDictionary *loginInfo, EMError *error) {
        
        if (!error) {
            
            NSLog(@"登录成功");
            
                                  
            AppDelegate *app = [UIApplication sharedApplication].delegate;
            
            [app  loginSuccess];
            
            //设置自动登录
            [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            
            
            
//            [[NSUserDefaults standardUserDefaults] setObject:self.textFieldOfZH forKey:@"userName"];
//            
//            [[NSUserDefaults standardUserDefaults] setObject:self.textFieldOfMM forKey:@"userPassWorld"];
            
            
        } else {
            
            // 显示错误信息的警告
            NSLog(@"%@",error);
        }
    } onQueue:dispatch_get_main_queue()];
    
    
    
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textFieldOfZH resignFirstResponder];
    [_textFieldOfMM resignFirstResponder];
}


-(void)ClickZuCe:(UIButton *)btn{
    
    RegisterVCOfFVQM *rvc = [[RegisterVCOfFVQM alloc]init];
    rvc.str = ^(NSString *str){
        
    self.textFieldOfZH.text = str;
        
    };
    rvc.string = ^(NSString *mystring){
        
    self.textFieldOfMM.text = mystring;
    
    };
    
    [self presentViewController:rvc animated:YES completion:nil];
    
    
       
}





#pragma mark - 第三方登录配置


-(void)ThridConfig{
    
    self.labelOfSanFang  = [[UILabel alloc]init];
    [self.view addSubview:_labelOfSanFang];
    self.labelOfSanFang.text = @"第三方账号登录";
    self.labelOfSanFang.textColor = [UIColor whiteColor];
    self.labelOfSanFang.font = [UIFont systemFontOfSize:12];
    [self.labelOfSanFang mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.buttonOfLogin.mas_bottom).with.offset(50);
        make.centerX.equalTo(self.view);
        make.width.equalTo(@120);
        make.height.equalTo(@30);
        
    }];
    
    
    self.buttonOfWX = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_buttonOfWX];
    self.buttonOfWX.layer.cornerRadius = 30;
    [self.buttonOfWX setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
    [self.buttonOfWX mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelOfSanFang.mas_bottom).with.offset(50);
        make.centerX.equalTo(self.view);
        make.width.equalTo(@60);
        make.height.equalTo(@60);
        
    }];
    
    
    
    
    self.buttonOfWB = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_buttonOfWB];
    self.buttonOfWB.layer.cornerRadius = 30;
    [self.buttonOfWB setImage:[UIImage imageNamed:@"微博(1)"] forState:  UIControlStateNormal];
    [self.buttonOfWB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelOfSanFang.mas_bottom).with.offset(50);
        make.right.equalTo(self.buttonOfWX.mas_left).with.offset(-30);
        make.width.equalTo(@60);
        make.height.equalTo(@60);
        
        
    }];
    
    
    self.buttonOfQQ = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_buttonOfQQ];
    self.buttonOfQQ.layer.cornerRadius = 30;
    [self.buttonOfQQ setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal];
    [self.buttonOfQQ mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelOfSanFang.mas_bottom).with.offset(50);
        make.left.equalTo(self.buttonOfWX.mas_right).with.offset(30);
        make.width.equalTo(@60);
        make.height.equalTo(@60);
        
        
    }];
    
    
}



@end
